package org.homly.pc.tag.product;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.homly.service.ProductService;
import org.homly.tool.StringTool;

/*
 * 查询配置
 */
@SuppressWarnings("serial")
public class TrolleyTag extends TagSupport {

	private String name;

	@SuppressWarnings("unchecked")
	@Override
	public int doStartTag() throws JspException {

		List<HashMap<String, String>> trolley = null;
		String productIdsS = "";
		double totalPrice = (double) 0;

		try {
			JSONObject[] trolleyJSONObjectArray = getTrolley();

			if (trolleyJSONObjectArray != null && trolleyJSONObjectArray.length > 0) {
				productIdsS = getProductIdsS(trolleyJSONObjectArray);
				HashMap para = new HashMap();
				para.put("productIds", productIdsS);
				para.put("status", (short) 1);
				trolley = ProductService.getProducts(para);
			}

			if (trolley != null && trolley.size() > 0) {
				for (int i = trolley.size() - 1; i >= 0; i --) {
					HashMap<String, String> product = trolley.get(i);
					Integer num = (Integer) trolleyJSONObjectArray[i].get("num");
					product.put("num", String.valueOf(num));
					String priceS = product.get("product_price");
					double price = Double.valueOf(priceS).doubleValue() * num;
					totalPrice += price;
					product.put("price", String.valueOf(price));
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		pageContext.setAttribute(name, trolley);
		pageContext.setAttribute(name + "_productIds", productIdsS);
		pageContext.setAttribute(name + "_totalPrice", totalPrice);

		return SKIP_BODY;
	}

	private JSONObject[] getTrolley() throws Exception {

		JSONObject[] trolleyJSONObject_Array = null;

		Cookie[] cookies = ((HttpServletRequest) pageContext.getRequest()).getCookies();
		String cookiesValue = null;
		String cookieName = null;
		Cookie cookie = null;
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				cookie = cookies[i];
				cookieName = cookie.getName();
				if (cookieName.equals("trolleyJSON")) {
					cookiesValue = StringTool.unescape(cookie.getValue());
				}
			}
		}

		if (cookiesValue != null && !"".equals(cookiesValue)) {
			JSONArray trolleyJSONArray = JSONArray.fromObject(cookiesValue);
			Object[] trolleyJSON_ObjectArray = trolleyJSONArray.toArray();
			trolleyJSONObject_Array = new JSONObject[trolleyJSON_ObjectArray.length];
			for (int i = trolleyJSONObject_Array.length - 1; i >= 0; i --) {
				trolleyJSONObject_Array[i] = JSONObject.fromObject(trolleyJSON_ObjectArray[i]);
			}
		}

		return trolleyJSONObject_Array;
	}

	private String getProductIdsS(JSONObject[] trolleyJSONObjectArray) {

		StringBuffer productIdsSB = new StringBuffer();

		for (int i = trolleyJSONObjectArray.length - 1; i >= 0; i --) {
			Integer productId = (Integer) trolleyJSONObjectArray[i].get("product_id");
			productIdsSB.append(productId);
			if (i > 0) {
				productIdsSB.append(",");
			}
		}

		return productIdsSB.toString();
	}

	public void setName(String name) {
		this.name = name;
	}
}
